<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px"
        ><!-- 左边的网站标志 -->
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="@/assets/logo.png" alt="logo" />
            <div class="title">智能云图库</div>
          </div>
        </router-link>
      </a-col>
      <!-- 中间的菜单 -->
      <a-col flex="auto">
        <!-- selectedKeys决定高亮哪些菜单项 -->
        <a-menu
          class="header-menu"
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <!-- 右边的用户头像 -->
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <!-- a-dropdown 组件除插槽组件外只允许有一个根元素 -->
            <a-dropdown>
              <!-- 可以给被包含的元素增加一些间距 -->
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.userAvatar"></a-avatar>
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </a-space>
              <!-- 插槽组件 -->
              <template #overlay>
                <a-menu class="avatarMenu">
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, AppstoreOutlined, LogoutOutlined, DiffOutlined,DropboxOutlined } from '@ant-design/icons-vue'
import { type MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'

const loginUserStore = useLoginUserStore()
const current = ref<string[]>(['mail'])
//未经过滤的菜单项
const originalItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    // key 是这个页面的唯一标识，这里为了方便让路由和key一致
    key: '/admin/userManage',
    icon: () => h(AppstoreOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    // key 是这个页面的唯一标识，这里为了方便让路由和key一致
    key: '/admin/pictureMange',
    icon: () => h(DropboxOutlined),
    label: '图片管理',
    title: '图片管理',
  },
  {
    // key 是这个页面的唯一标识，这里为了方便让路由和key一致
    key: '/add_picture',
    icon: () => h(DiffOutlined),
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: 'others',
    label: h('a', { href: 'https://codefather.cn', target: '_blank' }, '编程导航'),
    title: '编程导航',
  },
]

//展示菜单的数组‘
const items = computed(() => {
  return filterMenus(originalItems)
})

//过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  //?.启用链式调用
  return menus?.filter((menus) => {
    //管理员才能看到以admin开头的菜单项，只有管理员才能看到
    if (menus?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

const router = useRouter()
//菜单路由跳转函数
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

const doLogout = async () => {
  const logoutRes = await userLogoutUsingPost()
  if (logoutRes.data.code !== 0) {
    message.error('退出账号失败：' + logoutRes.data.message)
    return
  }
  loginUserStore.setLoginUser({
    userName: '未登录',
  })
  message.success('退出账号成功')
  await router.replace('user/login')
}
//路由的钩子函数，每次路由跳转后，更新当前高亮菜单项
router.afterEach((to, from, next) => {
  current.value = [to.path]
})
</script>
<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.logo {
  width: 48px;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 10px;
}

.header-menu {
  background-color: #3f87aa11;
}

.avatarMenu {
  background-color: #ace5ff;
}
</style>
